<?php

namespace app\controller;

use app\BaseController;
use app\model\AdminModel;
use think\facade\Db;
use think\facade\Session;
use think\Request;
use app\middleware\Check;
use think\response\Json;

/**
 * Class Storied 管理端 楼栋控制器
 * @package app\controller
 */
class Storied extends BaseController
{
    protected $middleware = [Check::class];

    /** 宿舍列表
     * @param Request $request
     */
    public function storied_list(Request $request)
    {
        try {
            if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
            $limit = $this->request->param('limit');
            $page = $this->request->param('page');
            $token = $request->param('token');
            $schoolId = $request->post('school_id');
            $admin =Admin_find($token);
            $map[] = ['s.operate_id', '=', $admin['id']];
            if ($schoolId != '') $map[] = ['d.school_id', '=', $schoolId];
            $table = Db::table('eb_school')->alias('s')
                ->join(['eb_dormitory' => 'd'], 'd.school_id = s.id')
                ->field('d.id,s.name as school_name,d.name,d.longitude,d.latitude,d.create_time,d.gender')
                ->where($map);
            $count = $table->count();
            if ($count == 0 ) return jsonSuccess('查询成功', ['count'=> $count, 'data' => []]);
            $list = $table->order('d.create_time', 'desc')->page($page, $limit)->select();
            return jsonSuccess('查询成功', ['count'=> $count, 'data' => $list]);
        } catch (\Exception $e) {
            return jsonFail('服务内部错误~');
        }
    }

    /** 简单宿舍列表
     * @param Request $request
     */
    public function simple_list(Request $request){
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $data = Db::table('eb_dormitory')->field('id,name')->select()->toArray();
        return jsonSuccess('获取成功！',['data'=>$data]);
    }
    /** 添加宿舍楼
     * @param Request $request
     */
    public function increase(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $name = $request->param('name');//联系人
        $token = $request->param('token');
        $gender = $request->param('gender');//0:男生宿舍 1：女生宿舍 2：混合宿舍
        $admin =Admin_find($token);
        // 获取学校id
        $schoolId = Db::table('eb_school')->where('operate_id', $admin['id'])->value('id');
        if ($schoolId === null) return jsonFail('获取学校信息失败');
        $longitude = $request->param('longitude');//经度
        $latitude = $request->param('latitude');//纬度
        if (empty($name)||$gender === null ||empty($longitude)||empty($latitude)) return jsonFail('缺少参数');
        $res = Db::table('eb_dormitory')->insertGetId([
            'name' => $name,
            'school_id' => $schoolId,
            'operate_id' => $admin['id'],
            'latitude' => $latitude,
            'longitude' => $longitude,
            'gender' =>$gender,
            'create_time' => time(),
        ]);
        if ($res) return jsonSuccess('添加成功！');
    }

    /**删除学校
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function delete_storied(Request $request){
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $token = $request->param('token');
        $admin =Admin_find($token);
        if($admin['type']!=1) return jsonFail('暂未权限操作，请联系系统管理员');
        $id = $request->param('id');//资方ID
        $res = Db::table('eb_dormitory')->where('id', $id)->delete();
        if($res) return jsonSuccess('删除成功！');
        return jsonFail('删除失败');
    }
    /**根据ID获取楼栋信息
     * @param Request $request
     */
    public function storied_find(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $id = $request->param('id');//资方ID
        $data = Db::table('eb_dormitory')->where('id', $id)->find();
        return jsonSuccess('获取成功！', $data);
    }

    /**编辑保存楼栋
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function storied_edit(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $id = $request->param('id');//id
        $name = $request->param('name');//联系人
        $school_id = $request->param('school_id');//学校ID
        $longitude = $request->param('longitude');//经度
        $latitude = $request->param('latitude');//纬度
        $gender = $request->param('gender');//0:男生宿舍 1：女生宿舍 2：混合宿舍
        if (empty($name)||$gender === null||empty($longitude)||empty($latitude)||empty($id)) return jsonFail('缺少参数');
        $res = Db::table('eb_dormitory')->where('id', $id)->update([
            'name' => $name,
            'school_id' => $school_id,
            'latitude' => $latitude,
            'gender' =>$gender,
            'longitude' => $longitude,
        ]);
        if($res==0)return jsonFail('数据没有任何变化，不做更改！');
        if ($res) return jsonSuccess('编辑成功！');
    }

    /**获取管理下的学校
     * @param Request $request
     * @return \think\response\Json
     */
    public function getOwnSchool(Request $request)
    {
        try{
            if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
            // 检测当前管理员状态
            $rtn = AdminModel::getInfo($request->post('token'), [1], [3]);
            if ($rtn instanceof Json) return $rtn;
            // 获取列表
            $table = Db::table('eb_school');
            $table->field('id, name');
            $map[] = ['operate_id', '=', $rtn['id']];
            $list = $table->where($map)->order('create_time', 'desc')->select();
            return jsonSuccess('查询成功', $list);
        } catch (\Exception $e) {
            return jsonFail('服务内部错误~');
        }
    }
}
